//
//  DCLoanListCell.m
//  Demo
//
//  Created by Kyle on 2023/1/1.
//

#import "DCLoanListCell.h"
#import "DCOrderModel.h"

@interface DCLoanListCell ()

@property (nonatomic, strong) UILabel *moneyLabel;

@property (nonatomic, strong) UIButton *statusBtn;

@property (nonatomic, strong) UILabel *dateLabel;

@property (nonatomic, strong) DCOrderModel *model;

@end

@implementation DCLoanListCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self configureUI];
    }
    return self;
}
- (void)configureData:(DCOrderModel *)model {
    _model = model;
    _moneyLabel.text = [NSString stringWithFormat:@"₦ %@",model.beforePaymentDetail.loanAmount];
    
    NSDate* date=[NSDate dateWithTimeIntervalSince1970:[model.beforePaymentDetail.applyTime longLongValue] /1000];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"dd-MM-yyyy"];
    NSString *dateStr = [formatter stringFromDate:date];
    _dateLabel.text = dateStr;
    
    //Under review 审核中
    //Transfering 放款中
    //Transfer failed 放款失败
    //Repaid 已还
    //Overdue 逾期待还款
    //Unpaid 待还款
    //Extension 展期
    //Rejected 被拒
    //状态90：审批中,100：审批通过,110：审批不通过,150：放款中,169：放款失败,170：放款成功,180：逾期,200：贷款结清
//    [_statusBtn setImage:[UIImage imageNamed:@"order-ziyuan627"] forState:UIControlStateNormal];
    [_statusBtn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [_statusBtn setTitleColor:[UIColor colorWithString:@"#1C221B"] forState:UIControlStateNormal];
    switch ([model.status intValue]) {
        case 90:
        {
            [_statusBtn setTitle:@"Under review" forState:UIControlStateNormal];
        }
            break;
//        case 100:
//        {
//            [_statusBtn setTitle:@"" forState:UIControlStateNormal];
//        }
//            break;
        case 110:
        {
            [_statusBtn setTitle:@"Rejected" forState:UIControlStateNormal];

        }
            break;
        case 150:
        {
            [_statusBtn setTitle:@"Transfering" forState:UIControlStateNormal];

        }
            break;
        case 169:
        {
            [_statusBtn setTitle:@"Transfer failed" forState:UIControlStateNormal];
            [_statusBtn setTitleColor:[UIColor colorWithString:@"#C70000"] forState:UIControlStateNormal];
        }
            break;
//        case 170:
//        {
//            [_statusBtn setTitle:@"" forState:UIControlStateNormal];
//
//        }
//            break;
        case 180:
        {
            [_statusBtn setTitle:@"Overdue" forState:UIControlStateNormal];

        }
            break;
        case 200:
        {
            [_statusBtn setTitle:@"Repaid" forState:UIControlStateNormal];
            [_statusBtn setImage:[UIImage imageNamed:@"order-ziyuan627"] forState:UIControlStateNormal];
        }
            break;
            
        default:
        {
            [_statusBtn setTitle:@"" forState:UIControlStateNormal];
        }
            break;
    }
}

- (void)configureUI {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
    UIView *bgView = [UIView new];
    bgView.layer.cornerRadius = 6;
    bgView.layer.masksToBounds = YES;
    bgView.backgroundColor = [UIColor colorWithString:@"#F7F7F7"];
    [self.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(5);
        make.bottom.mas_equalTo(-5);
        make.left.mas_equalTo(12);
        make.right.mas_equalTo(-12);
    }];
    
    UIImageView *colorImage = [UIImageView new];
    colorImage.backgroundColor = [UIColor colorWithString:@"#444342"];
    colorImage.layer.cornerRadius = 8;
    colorImage.layer.masksToBounds = YES;
    [bgView addSubview:colorImage];
    [colorImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(26);
        make.top.mas_equalTo(20);
        make.width.height.mas_equalTo(36);
    }];
    
    UILabel *moneyLabel = [UILabel new];
    _moneyLabel = moneyLabel;
    moneyLabel.text = @"₦10,000";
    moneyLabel.textColor = [UIColor colorWithString:@"#1C221B"];
    moneyLabel.font = DCFont_PF_Regular(14);
    [bgView addSubview:moneyLabel];
    [moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(26);
        make.top.mas_equalTo(colorImage.mas_bottom).mas_offset(18);
    }];
    
    UILabel *moneyDes = [UILabel new];
    moneyDes.text = @"Loan amount";
    moneyDes.textColor = [UIColor colorWithString:@"#CED7CD"];
    moneyDes.font = DCFont_PF_Regular(12);
    [bgView addSubview:moneyDes];
    [moneyDes mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(moneyLabel.mas_left);
        make.top.mas_equalTo(moneyLabel.mas_bottom).mas_offset(4);
    }];
    
    UIButton *statusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _statusBtn = statusBtn;
    [statusBtn setTitle:@"Rejected" forState:UIControlStateNormal];
    [statusBtn setTitleColor:[UIColor colorWithString:@"#1C221B"] forState:UIControlStateNormal];
    [_statusBtn addTarget:self action:@selector(statusBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    statusBtn.titleLabel.font = DCFont_PF_Regular(12);
    [bgView addSubview:statusBtn];
    [statusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-25);
        make.centerY.mas_equalTo(colorImage.mas_centerY);
    }];
    //使图片在右边，文字在左边（正常情况下是文字在右边，图片在左边）
    [_statusBtn setSemanticContentAttribute:UISemanticContentAttributeForceRightToLeft];
    //设置图片和文字之间的间隙
    _statusBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    
    UILabel *dateLabel = [UILabel new];
    _dateLabel = dateLabel;
    dateLabel.textColor = [UIColor colorWithString:@"#1C221B"];
    dateLabel.text = @"18-03-2022";
    dateLabel.font = DCFont_PF_Regular(14);
    [bgView addSubview:dateLabel];
    [dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(statusBtn.mas_right);
        make.centerY.mas_equalTo(moneyLabel.mas_centerY);
    }];
    
    UILabel *dateDes = [UILabel new];
    dateDes.text = @"Application date";
    dateDes.textColor = [UIColor colorWithString:@"#CED7CD"];
    dateDes.font = DCFont_PF_Regular(12);
    [bgView addSubview:dateDes];
    [dateDes mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(dateLabel.mas_right);
        make.centerY.mas_equalTo(moneyDes.mas_centerY);
    }];
    
}

- (void)statusBtnAction:(UIButton *)sender {
    
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
